package com.zy.je.common.mybatis.ext.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zy.je.common.core.annotation.ApiLog;
import com.zy.je.common.core.utils.CheckUtil;
import com.zy.je.common.core.web.rest.RestResult;
import com.zy.je.common.mybatis.ext.model.base.BaseEntity;
import com.zy.je.common.mybatis.ext.service.BizService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Authorization;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zy
 */
@Slf4j
public abstract class BizBaseController<T extends BaseEntity> {
    @Autowired
    protected BizService<T> baseService;

    @InitBinder
    public void customizeBinding(WebDataBinder binder) {
        //解决下划级开头的参数接收不到的问题
        //required to handle underscore prefixed fields ("_field")
        binder.setFieldMarkerPrefix(null);
    }

    @GetMapping({"/_count"})
    @ApiOperation(value = "记录数")
    public RestResult<Long> bizCount(@ApiParam("查询条件") T entity) throws IllegalAccessException {
        return RestResult.ok(baseService.bizCount(entity));
    }

    @GetMapping({"/_get"})
    @ApiOperation(value = "根据id查询数据")
    public RestResult<T> bizGet(@RequestParam String id) {
        return RestResult.ok(this.baseService.bizGet(id));
    }

    @GetMapping("/_page")
    @ApiOperation(value = "分页查询")
    public RestResult<IPage<T>> bizPageList(@ApiParam("查询条件") T m) {
        return RestResult.ok(baseService.bizPageList(m));
    }

//    @GetMapping({"/_all"})
//    @ApiOperation(value = "查询")
//    public RestResult<List<T>> all(@ApiParam("查询条件") T entity) throws IllegalAccessException {
//        List<T> rs = this.baseService.list(BaseWrapper.baseQueryWrapper(entity));
//        return RestResult.ok(rs);
//    }

    @PostMapping("_create")
    @ApiOperation(value = "新增")
    @ApiLog("新增")
    public RestResult<String> bizCreate(@ApiParam("新增") @RequestBody @Validated T entity) {
        return RestResult.ok(this.baseService.bizCreate(entity), "新增成功");
    }

    @PutMapping({"/_update"})
    @ApiOperation(value = "根据id更新")
    @ApiLog("根据id更新")
    public RestResult<String> bizUpdate(@RequestBody @Validated T entity) {
        if (CheckUtil.isBlank(entity.getId())) {
            return RestResult.fail(null, "ID不能为空");
        }
        return RestResult.ok(this.baseService.bizUpdate(entity), "更新完成");
    }

    @PostMapping(value = "/_save")
    @ApiOperation(value = "保存（id为空则新增/id不为空则更新）")
    public RestResult<String> bizSave(@RequestBody @Validated T entity) {
        return RestResult.ok(this.baseService.bizSave(entity), "保存完成");
    }

    @DeleteMapping({"/_delete/{id}"})
    @ApiOperation(value = "根据id删除", authorizations = {@Authorization("apiKey")})
    @ApiLog("根据id删除")
    public RestResult<String> bizDelete(@PathVariable("id") String id) {
        return RestResult.ok(this.baseService.bizDelete(id), "删除完成");
    }


    @PostMapping(value = "/_get/batch")
    @ApiOperation(value = "根据id拼串批量查询数据")
    public RestResult<List<T>> bizBatchGet(String ids) {
        return RestResult.ok(this.baseService.bizBatchGet(ids));
    }

    @PostMapping(value = "/_save/batch")
    @ApiOperation(value = "批量保存（id为空则新增/id不为空则更新）")
    public RestResult<List<String>> bizBatchSave(@RequestBody @Validated List<T> dataList) {
        return RestResult.ok(this.baseService.bizBatchSave(dataList));
    }

    @DeleteMapping({"/_delete/batch"})
    @ApiOperation(value = "根据id拼串串批量删除")
    @ApiLog("根据ID字符串批量删除")
    public RestResult<List<String>> deleteBatch(String ids) {
        return RestResult.ok(this.baseService.bizBatchDelete(ids), "批量删除成功");
    }

}
